package nicg.st.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class DBUtil {
	private static String driver;
	
	private static String url;
	
	private static String user;
	
	private static String password;
	
	static {
		try {
			Properties properties = new Properties();
			
			InputStream inputStream = DBUtil.class.getResourceAsStream("/db.properties");
			properties.load(inputStream);
			
			driver = properties.getProperty("jdbc.driver");
			url = properties.getProperty("jdbc.url");
			user = properties.getProperty("jdbc.user");
			password = properties.getProperty("jdbc.password");
			
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() {
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
	
	public static void releaseDB(Connection connection, Statement statement, ResultSet resultSet){
		try {
			if(connection != null) connection.close();
			if(statement != null) statement.close();
			if(resultSet != null) resultSet.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void releaseDB(Connection connection, Statement statement){
		try {
			if(connection != null) connection.close();
			if(statement != null) statement.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
